/**
 * Copyright (C), 2015-2018, XXX有限公司
 * FileName: DictFieldWarpperFactory
 * Author:   zhouheng
 * Date:     2018/6/24 18:25
 * Description: 字典字段映射包装类工厂
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.hengby.hengcrud.core.common.dictmap.factory;

import com.hengby.hengcrud.core.common.factory.ConstantFactory;
import com.hengby.hengcrud.core.common.factory.IConstantFactory;
import com.hengby.hengcrud.core.exception.HengCrudException;
import com.hengby.hengcrud.core.exception.HengCrudExceptionEnum;

import java.lang.reflect.Method;

/**
 * 〈一句话功能简述〉<br> 
 * 〈字典字段映射包装类工厂〉
 *
 * @author zhouheng
 * @create 2018/6/24
 * @since 1.0.0
 */
public class DictFieldWarpperFactory {


    // 根据key值 找到对应的方法名称并利用反射机制调用
    public static Object createFieldWarpper(Object key, String dictMethodName) {
        IConstantFactory constantFactory = ConstantFactory.me();
        try {
            Method method = constantFactory.
                    getClass().getMethod(dictMethodName,key.getClass());
            if (method == null){
                throw new HengCrudException(HengCrudExceptionEnum.ERROR_WRAPPER_METHOD_NOTFOUND);
            }else {
                return method.invoke(constantFactory,key);
            }
        } catch (Exception e) {
            throw new HengCrudException(HengCrudExceptionEnum.ERROR_WRAPPER_FIELD);
        }
    }
}